package com.cheejee.goodscenter.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.carry.common.tool.StringTool;
import com.cheejee.goodscenter.dao.CategoryPropertiesTemplateMapper;
import com.cheejee.goodscenter.dao.GoodsCategoryPropertiesMapper;
import com.cheejee.goodscenter.module.entity.CategoryPropertiesTemplate;
import com.cheejee.goodscenter.service.CategoryPropertiesTemplateService;
import com.cheejee.goodscenter.service.exception.CheckException;
import com.cheejee.goodscenter.service.exception.DataNotExistException;
import com.cheejee.goodscenter.service.exception.DuplicateException;
import com.cheejee.goodscenter.validated.Check;
import com.cheejee.goodscenter.validated.InsertGroup;
import com.cheejee.goodscenter.validated.UpdateGroup;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.collection.CollectionUtil;

/**
 * @author CARRY
 * @Date 2020年10月26日下午7:05:39
 */
@Service
public class CategoryPropertiesTemplateServiceImpl implements CategoryPropertiesTemplateService {

	@Resource
	private CategoryPropertiesTemplateMapper cptMapper;

	@Resource
	private GoodsCategoryPropertiesMapper gcpMapper;

	@Override
	public Page<CategoryPropertiesTemplate> queryCategoryPropertiesTemplate(Integer categoryPropertiesGroupId, Integer categoryId, String propertiesName, Boolean required, Byte type, String initial, int pageNum,
			int pageSize) {

		return PageHelper.startPage(pageNum, pageSize).doSelectPage(() -> cptMapper.queryBySelective(categoryId, categoryPropertiesGroupId, propertiesName, required, type, initial));
	}

	@Override
	public CategoryPropertiesTemplate queryCategoryPropertiesTemplateById(Integer id) {

		return cptMapper.selectByPrimaryKey(id);
	}

	@Override
	@Check(message = "新增的分类属性模板不能为空", group = InsertGroup.class)
	public int addCategoryPropertiesTemplate(CategoryPropertiesTemplate propertiesTemplate) {

		if (!CollectionUtil.isEmpty(cptMapper.queryBySelective(null, null, propertiesTemplate.getPropertiesName(), null, null, null))) {
			throw new DuplicateException("新增的分类属性模板名称重复");
		}

		propertiesTemplate.setInitial(StringTool.getInitial(propertiesTemplate.getPropertiesName()));

		return cptMapper.insertSelective(propertiesTemplate);
	}

	@Override
	@Check(message = "修改的分类属性模板不能为空", group = UpdateGroup.class)
	public int changeCategoryPropertiesTemplate(CategoryPropertiesTemplate propertiesTemplate) {
		
		checkExist(propertiesTemplate.getId());
		checkNameChange(propertiesTemplate);
		List<CategoryPropertiesTemplate> lb = cptMapper.queryBySelective(null, null, propertiesTemplate.getPropertiesName(), null, null, null);

		if (!CollectionUtil.isEmpty(lb) && lb.get(0).getId() != propertiesTemplate.getId()) {
			throw new DuplicateException("修改的分类属性模板名称重复");
		}

		propertiesTemplate.setInitial(StringTool.getInitial(propertiesTemplate.getPropertiesName()));

		return cptMapper.updateByPrimaryKeySelective(propertiesTemplate);
	}

	@Override
	public int deleteCategoryPropertiesTemplate(Integer... id) {

		return cptMapper.deleteById(id);
	}

	private void checkNameChange(CategoryPropertiesTemplate pt) {
		if (!CollectionUtil.isEmpty(gcpMapper.selectBySelective(null, pt.getId(), null))) {
			String oldName = cptMapper.selectByPrimaryKey(pt.getId()).getPropertiesName();
			String newName = pt.getPropertiesName();
			if(!oldName.equals(newName)) {
				throw CheckException.builder().checkMessage("模板已被使用，不能修改属性名称").build();
			}
		}
	}
	
	private void checkExist(Integer id) {
		if(cptMapper.selectByPrimaryKey(id) == null) {
			throw new DataNotExistException("分类属性模板不存在");
		}
	}

}
